package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MarketingBusiness;
import com.ruoyi.system.service.IMarketingBusinessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;

/**
 * 营销业务办理Controller
 * 
 * @author Alex
 * @date 2025-07-16
 */
@RestController
@RequestMapping("/system/business")
public class MarketingBusinessController extends BaseController
{
    @Autowired
    private IMarketingBusinessService marketingBusinessService;

    /**
     * 查询营销业务办理列表
     */
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/list")
    public TableDataInfo list(MarketingBusiness marketingBusiness)
    {
        startPage();
        List<MarketingBusiness> list = marketingBusinessService.selectMarketingBusinessList(marketingBusiness);
        return getDataTable(list);
    }

    /**
     * 导出营销业务办理列表
     */
    @PreAuthorize("@ss.hasPermi('system:business:export')")
    @Log(title = "营销业务办理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MarketingBusiness marketingBusiness)
    {
        List<MarketingBusiness> list = marketingBusinessService.selectMarketingBusinessList(marketingBusiness);
        ExcelUtil<MarketingBusiness> util = new ExcelUtil<MarketingBusiness>(MarketingBusiness.class);
        util.exportExcel(response, list, "营销业务办理数据");
    }

    /**
     * 获取营销业务办理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(marketingBusinessService.selectMarketingBusinessById(id));
    }

    /**
     * 新增营销业务办理
     */
    @PreAuthorize("@ss.hasPermi('system:business:add')")
    @Log(title = "营销业务办理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MarketingBusiness marketingBusiness)
    {
        return toAjax(marketingBusinessService.insertMarketingBusiness(marketingBusiness));
    }

    /**
     * 修改营销业务办理
     */
    @PreAuthorize("@ss.hasPermi('system:business:edit')")
    @Log(title = "营销业务办理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MarketingBusiness marketingBusiness)
    {
        return toAjax(marketingBusinessService.updateMarketingBusiness(marketingBusiness));
    }

    /**
     * 删除营销业务办理
     */
    @PreAuthorize("@ss.hasPermi('system:business:remove')")
    @Log(title = "营销业务办理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(marketingBusinessService.deleteMarketingBusinessByIds(ids));
    }

    /**
     * 统计各套餐类型数量
     */

    @PostMapping("/countPackageType")
    public AjaxResult countPackageType(@RequestBody Map<String, Object> params) {
        Long deptId = params.get("deptId") != null ? Long.valueOf(params.get("deptId").toString()) : null;
        Long marketingUserId = params.get("marketingUserId") != null ? Long.valueOf(params.get("marketingUserId").toString()) : null;
        String startDate = params.get("startDate") != null ? params.get("startDate").toString() : null;
        String endDate = params.get("endDate") != null ? params.get("endDate").toString() : null;
        List<Map<String, Object>> result = marketingBusinessService.countPackageType(deptId, marketingUserId, startDate, endDate);
        return AjaxResult.success(result);
    }

    /**
     * 根据套餐类型统计各营业部下套餐数量
     */
    @PostMapping("/countByPackageTypeAndDept")
    public AjaxResult countByPackageTypeAndDept(@RequestBody Map<String, Object> params) {
        String packageType = params.get("packageType") != null ? params.get("packageType").toString() : null;
        String effectiveDate = params.get("effectiveDate") != null ? params.get("effectiveDate").toString() : null;
        String expireDate = params.get("expireDate") != null ? params.get("expireDate").toString() : null;
        Long deptId = params.get("deptId") != null ? Long.valueOf(params.get("deptId").toString()) : null;
        List<Map<String, Object>> result = marketingBusinessService.countByPackageTypeAndDept(packageType, effectiveDate, expireDate,deptId);
        return AjaxResult.success(result);
    }

    /**
     *右侧小区排行榜
     */
    @PostMapping("/countByRanking")
    public AjaxResult countByRanking(@RequestBody Map<String, Object> params) {
        Long deptId = params.get("deptId") != null ? Long.valueOf(params.get("deptId").toString()) : null;
        String effectiveDate = params.get("effectiveDate") != null ? params.get("effectiveDate").toString() : null;
        String expireDate = params.get("expireDate") != null ? params.get("expireDate").toString() : null;
        List<Map<String, Object>> result = marketingBusinessService.countByRanking(deptId, effectiveDate, expireDate);
        return AjaxResult.success(result);
    }

    /**
     *上侧排行榜
     */
    @PostMapping("/countByPermeability")
    public AjaxResult countByPermeability(@RequestBody Map<String, Object> params) {
        Long deptId = params.get("deptId") != null ? Long.valueOf(params.get("deptId").toString()) : null;
        List<Map<String, Object>> result = marketingBusinessService.countByPermeability(deptId);
        return AjaxResult.success(result);
    }
}
